package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.EventAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 活动界面
 * 
 * @author lkx Created at 2015-10-26 上午11:36:10
 * @version 1.0
 */
public class EventActivity extends BaseActivity implements OnCheckedChangeListener {
	final static String TAG = "EventActivity";
	private ActivityTitle title;
	RadioButton inProcess, takePartIn;
	private String url;
	private PullToRefreshListView eventListView;
	private int page = 1;
	private int type = 1;// 加载数据类型默认时进行中
	private long time;
	private EventAdapter adapter;
	private JSONArray array;
	private TextView nodate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_event);
		initView();
	}

	/**
	 * 绑定控件
	 * 
	 * 
	 * @author lkx Created at 2015-10-26 下午2:09:55
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.eventActivityTitle);
		title.setTitle("活动");
		title.setViewGone(title.rightLayout, title.titleRefresh, title.titleMsg);
		title.setViewVersible(title.titleBack);
		nodate = (TextView) findViewById(R.id.noEventData);
		inProcess = (RadioButton) findViewById(R.id.infox_inProgress);
		takePartIn = (RadioButton) findViewById(R.id.infox_takePartIn);
		eventListView = (PullToRefreshListView) findViewById(R.id.eventListview);
		inProcess.setChecked(true);// 默认选择正在进行
		inProcess.setOnCheckedChangeListener(this);
		takePartIn.setOnCheckedChangeListener(this);

		eventListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				time = 0;
				getData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				getData();
			}

		});
		adapter = new EventAdapter(EventActivity.this);
		eventListView.setAdapter(adapter);

	//	getData();
		eventListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(EventActivity.this, EventDetailActivity.class);
				try {
					intent.putExtra("title", array.getJSONObject(position - 1).getString("title"));
					intent.putExtra("state", array.getJSONObject(position - 1).getString("state"));
					intent.putExtra("id", array.getJSONObject(position - 1).getString("id"));
					intent.putExtra("isJoin", array.getJSONObject(position - 1).getString("join"));
					intent.putExtra("gid", array.getJSONObject(position - 1).getString("gid"));
					intent.putExtra("rid", array.getJSONObject(position - 1).getString("rid"));
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	@Override
	public void onCheckedChanged(CompoundButton button, boolean ischeck) {
		page = 1;
		if (ischeck) {
			switch (button.getId()) {
			case R.id.infox_inProgress:
				type = 1;
				getData();
				break;
			case R.id.infox_takePartIn:
				type = 2;
				getData();
				break;
			}

		}
	}

	/**
	 * 加载数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-26 下午3:00:36
	 */
	private void getData() {
		ApiParam param = ApiParam.create();
		param.addParam("page", page);
		param.addParam("type", type);
		if (time > 0) {
			param.addParam("time", time);
		}

		Api.get("activity/list", param, new ApiCallback(EventActivity.this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						nodate.setVisibility(View.VISIBLE);
						eventListView.setVisibility(View.GONE);

					} else {
						page--;
					}

				} else {
					if (page == 1) {
						if (result.getDatas() == null || result.getDatas().length() == 0) {
							nodate.setVisibility(View.VISIBLE);
							eventListView.setVisibility(View.GONE);
						} else {
							nodate.setVisibility(View.GONE);
							eventListView.setVisibility(View.VISIBLE);
						}
						array = result.getDatas();
						adapter.setData(result.getDatas());
					} else {
						try {
							array = AppKit.joinJSONArray(array, result.getDatas());
						} catch (JSONException e) {
							Log.e(TAG, e.getMessage(), e);
						}
						adapter.appendData(result.getDatas());
					}
					time = result.getTime();
				}

				eventListView.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {

			}
		}, EventActivity.this);
	}
}
